#include <stdio.h>

void escape(char s[], char t[]);
int countescapes(char s[]);
int main()
{
  char s[100];
  escape(s, "lol what is this bullshit \b \b \n \r \n \\ \n");
  printf("%s \n number of escapes: %d \n",s,countescapes(s));
  
}

int countescapes(char s[])
{
  int i,j;
  i=j=0;
  while (s[i]!=0)
  {
    switch(s[i])
	 {
	 case '\0' : 
	   j++;
	   i++;
	   continue;
	 case '\a' : 
	   j++;
	   i++;
	   continue;
	 case '\b' : 
	   j++;
	   i++;
	   continue;
	 case '\t' :
	   j++;
	   i++;
	   continue;
	 case '\n' : 
	   j++;
	   i++;
	   continue;
	 case '\v' : 
	   j++;
	   i++;
	   continue;
	 case '\f' : 
	   j++;
	   i++;
	   continue;
	 case '\r' : 
	   j++;
	   i++;
	   continue;
	 case '\\':
	   j++;
	   i++;
	   continue;
	 default:  i++; 
	 }
  }
  return j-1; //end of the string always matches
}  
/*
  s must be big enough to hold a string from t with all the escapes in
*/
void escape(char s[], char t[])
{
  int i,j;
  i=j=0;
  while(t[j]!=0)
    {
	 switch(t[j])
	   {
	   case '\0' : 
		s[i]='\\';
		s[i+1]='0';
		i+=2;
		j++;
		continue;
	   case '\a' : 
		s[i]='\\';
		s[i+1]='a';
		i+=2;
		j++;
		continue;
	   case '\b' : 
		s[i]='\\';
		s[i+1]='b';
		i+=2;
		j++;
		continue;
	   case '\t' :
		s[i]='\\';
		s[i+1]='t';
		i+=2;
		j++;
		continue;
	   case '\n' : 
		s[i]='\\';
		s[i+1]='n';
		i+=2;
		j++;
		continue;
	   case '\v' : 
		s[i]='\\';
		s[i+1]='v';
		i+=2;
		j++;
		continue;
	   case '\f' : 
		s[i]='\\';
		s[i+1]='f';
		i+=2;
		j++;
		continue;
	   case '\r' : 
		s[i]='\\';
		s[i+1]='r';
		i+=2;
		j++;
		continue;
	   case '\\':
		s[i]='\\';
		s[i+1]='\\';
		i+=2;
		j++;
		continue;
	   default: s[i]=t[j]; i++; j++;
	   }
    }
  s[i]=0;
}

void deescape(char s[], char t[])
{
  int i,j;
  i=j=0;
  while(t[j]!=0)
    {
	 switch(t[j])
	   {
	   case '\0' : 
		s[i]='\\';
		s[i+1]='0';
		i+=2;
		j++;
		continue;
	   case '\a' : 
		s[i]='\\';
		s[i+1]='a';
		i+=2;
		j++;
		continue;
	   case '\b' : 
		s[i]='\\';
		s[i+1]='b';
		i+=2;
		j++;
		continue;
	   case '\t' :
		s[i]='\\';
		s[i+1]='t';
		i+=2;
		j++;
		continue;
	   case '\n' : 
		s[i]='\\';
		s[i+1]='n';
		i+=2;
		j++;
		continue;
	   case '\v' : 
		s[i]='\\';
		s[i+1]='v';
		i+=2;
		j++;
		continue;
	   case '\f' : 
		s[i]='\\';
		s[i+1]='f';
		i+=2;
		j++;
		continue;
	   case '\r' : 
		s[i]='\\';
		s[i+1]='r';
		i+=2;
		j++;
		continue;
	   case '\\':
		s[i]='\\';
		s[i+1]='\\';
		i+=2;
		j++;
		continue;
	   default: s[i]=t[j]; i++; j++;
	   }
    }
  s[i]=0;
}
